// 将所需要的请求方法引入到文件中
import { reqLoginData, reqOutLogin } from "@/api"

const state = {
  userDate: {
    username: localStorage.getItem('username'),
    token: localStorage.getItem('token'),
    // userName: '',
    // token: ''
  }
}

const actions = {
  async getLoginData({ commit }, user) {
    const result = await reqLoginData(user)
    console.log(result);
    if (result.code == 200) {
      localStorage.setItem('username', result.data.name)
      localStorage.setItem('token', result.data.token)
      commit('GET_LOGIN_DATA', result.data)
    }
    else {
      console.log(result.message);
    }
  },

  // 发送请求删除服务器中信息
  async getOutLogin({ commit }) {
    const result = await reqOutLogin();
    console.log(result);
    if (result.code == 200) {
      localStorage.removeItem("username");
      localStorage.removeItem("token");
      commit("GET_OUT_LOGIN")
    } else {
      console.log(result.message);
    }
  }
}


const mutations = {
  // 点击登录获取到用户名和token进行校验
  GET_LOGIN_DATA(state, ployed) {
    state.userDate.username = ployed.name
    state.userDate.token = ployed.token
  },

  // 点击退出将用户名和token清除
  GET_OUT_LOGIN(state) {
    state.userDate.username = '';
    state.userDate.token = '';
  }
}

const getters = {
}

export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters
}